﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectBordspil.Models;

namespace ProjectBordspil.Sevices
{
    public class HomeServices
    {
        private IAppRepository _repository = null;

        public HomeServices()
        {
            _repository = new AppRepository();
        }

        public HomeServices(IAppRepository rep)
        {
            _repository = rep;
        }

        /// <summary>
        /// Gets list of top 5 score from Guillonetine and Memory game
        /// </summary>
        public ListOfTopPlayers TopFive()
        {
            ListOfTopPlayers listOfTopFive = new ListOfTopPlayers();

            listOfTopFive.TopFiveGui = (from p in _repository.GetAllPlayers
                                        orderby p.scoreGui descending
                                        select p).Take(5);

            listOfTopFive.TopFiveMem = (from p in _repository.GetAllPlayers
                                        orderby p.scoreMem descending
                                        select p).Take(5);
            return listOfTopFive;
        }

        /// <summary>
        /// Gets all games 
        /// </summary>
        public IEnumerable<GameModel> GetAllGames()
        {
            return _repository.GetAllGames;
        }

        /// <summary>
        /// Gets user
        /// </summary>
        public UserModel GetUser(string userName)
        {
            UserModel user = (from i in _repository.GetAllPlayers 
                              where i.userName == userName 
                              select i).SingleOrDefault();
            return user;
        }

        /// <summary>
        /// Sets up my page
        /// </summary>
        public MyViewModel SetUpMyPage(string userName)
        {
            MyViewModel myModel = new MyViewModel();

            myModel.myStatus = GetUser(userName);

            myModel.myGame = from i in _repository.GetActiveGames where i.user1 == userName || i.user2 == userName select i;

            return myModel;
        }

        /// <summary>
        /// Gets all active games
        /// </summary>
        public IEnumerable<ActiveGames> GetActiveGames()
        {
            var games = from game in _repository.GetActiveGames
                        where game.finished == false
                        select game;

            return games;
        }
    }
}